
#include "compile_run.hpp"
#include "../comm/httplib.h"

using namespace ns_compile_and_run;
using namespace httplib;

void Usage(const std::string &proc)
{
    std::cerr << "Usage:\n\t" << proc << " port" << std::endl;
}

// 编译服务随时可能被多个人请求，必须保证传递上来的code，形成源文件名称的时候，要具有
// 唯一性，要不然多个用户之间会互相影响
int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    Server svr;

    svr.Get("/hello", [](const Request &req, Response &resp)
            { resp.set_content("hello httplib, 中午好！", "text/plain;charset=utf-8"); });

    svr.Post("/compile_and_run", [](const Request &req, Response &resp)
             {
                 std::string in_json = req.body;
                 std::string out_json;
                 CompileAndRun::Start(in_json, &out_json);
                 resp.set_content(out_json, "application/json;charset=utf-8"); });

    svr.listen("0.0.0.0", std::stoi(argv[1]));
    return 0;
}
